package com.store.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.store.entity.RefundRecord;
import com.store.vo.RefundRecordVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 退货记录Mapper
 */
@Mapper
public interface RefundRecordMapper extends BaseMapper<RefundRecord> {
    
    /**
     * 根据退货ID查询详情（包含订单和用户信息）
     */
    @Select("SELECT " +
            "r.id, r.order_id, r.refund_amount, r.refund_reason, r.status, " +
            "r.handler_id, r.handle_time, r.handle_remark, r.create_time, r.update_time, " +
            "o.order_no, o.user_id, " +
            "u.username, u.name, " +
            "h.name as handler_name " +
            "FROM refund_records r " +
            "LEFT JOIN orders o ON r.order_id = o.id " +
            "LEFT JOIN users u ON o.user_id = u.id " +
            "LEFT JOIN users h ON r.handler_id = h.id " +
            "WHERE r.id = #{refundId}")
    RefundRecordVO selectRefundDetailById(@Param("refundId") Long refundId);
    
    /**
     * 根据订单ID查询退货记录
     */
    @Select("SELECT " +
            "r.id, r.order_id, r.refund_amount, r.refund_reason, r.status, " +
            "r.handler_id, r.handle_time, r.handle_remark, r.create_time, r.update_time, " +
            "o.order_no, o.user_id, " +
            "u.username, u.name, " +
            "h.name as handler_name " +
            "FROM refund_records r " +
            "LEFT JOIN orders o ON r.order_id = o.id " +
            "LEFT JOIN users u ON o.user_id = u.id " +
            "LEFT JOIN users h ON r.handler_id = h.id " +
            "WHERE r.order_id = #{orderId}")
    RefundRecordVO selectByOrderId(@Param("orderId") Long orderId);
    
    /**
     * 根据用户ID查询退货记录列表
     */
    @Select("SELECT " +
            "r.id, r.order_id, r.refund_amount, r.refund_reason, r.status, " +
            "r.handler_id, r.handle_time, r.handle_remark, r.create_time, r.update_time, " +
            "o.order_no, o.user_id, " +
            "u.username, u.name, " +
            "h.name as handler_name " +
            "FROM refund_records r " +
            "LEFT JOIN orders o ON r.order_id = o.id " +
            "LEFT JOIN users u ON o.user_id = u.id " +
            "LEFT JOIN users h ON r.handler_id = h.id " +
            "WHERE o.user_id = #{userId} " +
            "ORDER BY r.create_time DESC")
    List<RefundRecordVO> selectByUserId(@Param("userId") Long userId);
}





